﻿package com.sh.game.loader
{
    import __AS3__.vec.*;
    import com.sh.game.consts.*;
    import com.sh.game.global.*;
    import com.sh.game.util.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;

    public class MovieAssets extends Object
    {
        private var _loader:Loader;
        private var callBack:Function;
        private var _xml:XML;
        private var _args:Array;
        public var frameNames:Array;
        private var _curIndex:int = 0;
        private var imgBytes:Object;
        private var _imgs:Object;
        private var _curname:String;
        public var frameSet:Vector.<BitmapData>;

        public function MovieAssets()
        {
            return;
        }// end function

        public function get imgs() : Object
        {
            return this._imgs;
        }// end function

        public function get xml() : XML
        {
            return this._xml;
        }// end function

        public function read(param1:ByteArray, param2:Function, param3:Array) : void
        {
            var len:int;
            var i:int;
            var xmlbytes:ByteArray;
            var xlen:int;
            var names:String;
            var size:int;
            var bs:ByteArray;
            var bytes:* = param1;
            var callback:* = param2;
            var args:* = param3;
            if (bytes == null)
            {
                if (Config.Log > LogType.ERROR)
                {
                    Logger.log("特效资源加载出错" + args);
                }
                if (this.callBack != null)
                {
                    this.callBack.apply(null, this._args != null ? (this._args.concat(null)) : ([null]));
                }
                this.dispose();
                return;
            }
            try
            {
                this.callBack = callback;
                this._args = args;
                bytes.position = 0;
                len = bytes.readInt();
                this.frameNames = new Array();
                this.imgBytes = new Object();
                this._imgs = new Object();
                i;
                while (i < len)
                {
                    
                    names = bytes.readUTF();
                    this.frameNames.push(names);
                    size = bytes.readInt();
                    bs = new ByteArray();
                    bytes.readBytes(bs, 0, size);
                    this.imgBytes[names] = bs;
                    i = (i + 1);
                }
                xmlbytes = new ByteArray();
                xlen = bytes.readInt();
                bytes.readBytes(xmlbytes);
                xmlbytes.uncompress();
                this._xml = new XML(xmlbytes);
                this.loadBmp();
            }
            catch (error:Error)
            {
                if (Config.Log > LogType.ERROR)
                {
                    Logger.log("特效资源解析出错" + args);
                }
                if (this.callBack != null)
                {
                    this.callBack.apply(null, _args != null ? (_args.concat(null)) : ([null]));
                }
                this.dispose();
            }
            return;
        }// end function

        private function loadBmp() : void
        {
            this._curIndex = 0;
            if (!this._loader)
            {
                this._loader = new Loader();
            }
            this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.bmpLoadComplete);
            this.loadNext();
            return;
        }// end function

        private function loadNext() : void
        {
            var _loc_1:ByteArray = null;
            var _loc_2:int = 0;
            if (this._curIndex < this.frameNames.length)
            {
                this._curname = this.frameNames[this._curIndex];
                _loc_1 = this.imgBytes[this._curname];
                this._loader.loadBytes(_loc_1);
                _loc_1.clear();
                delete this.imgBytes[this._curname];
                var _loc_3:String = this;
                var _loc_4:* = this._curIndex + 1;
                _loc_3._curIndex = _loc_4;
            }
            else
            {
                this.frameSet = new Vector.<BitmapData>;
                this.frameNames.sort();
                _loc_2 = 0;
                while (_loc_2 < this.frameNames.length)
                {
                    
                    this.frameSet[_loc_2] = this._imgs[this.frameNames[_loc_2]];
                    _loc_2++;
                }
                this._loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, this.bmpLoadComplete);
                this._loader.unloadAndStop(false);
                this._loader = null;
                if (this.callBack != null)
                {
                    this.callBack.apply(null, this._args != null ? (this._args.concat(this)) : ([this]));
                }
            }
            return;
        }// end function

        private function bmpLoadComplete(event:Event) : void
        {
            var _loc_2:* = event.currentTarget.content;
            this._imgs[this._curname] = _loc_2.bitmapData;
            this.loadNext();
            return;
        }// end function

        public function dispose() : void
        {
            var _loc_1:String = null;
            if (this.imgBytes)
            {
                for each (_loc_1 in this.imgBytes)
                {
                    
                    if (this.imgBytes[_loc_1])
                    {
                        this.imgBytes[_loc_1].clear();
                    }
                    delete this.imgBytes[_loc_1];
                }
                this.imgBytes = null;
            }
            if (this.frameNames)
            {
                this.frameNames = null;
            }
            if (this._loader)
            {
                this._loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, this.bmpLoadComplete);
                this._loader.unloadAndStop(false);
                this._loader = null;
            }
            if (this._imgs)
            {
                this._imgs = null;
            }
            this.callBack = null;
            this.frameSet = null;
            this._xml = null;
            this._args = null;
            return;
        }// end function

    }
}
